import 'package:custom_widgets/item/item_normal_widget.dart';
import 'package:flutter/material.dart';
// import 'package:launch_review/launch_review.dart';
import 'package:mi_mang_flutter/manager/app_manager.dart';
import 'package:mi_mang_flutter/page/login/login_router.dart';
import 'package:mi_mang_flutter/page/setting/setting_router.dart';
import 'package:mi_mang_flutter/provider/app_theme.dart';
import 'package:mi_mang_flutter/provider/theme_provider.dart';
import 'package:mi_mang_flutter/provider/user_provider.dart';
import 'package:mi_mang_flutter/res/dimens.dart';
import 'package:mi_mang_flutter/res/styles.dart';
import 'package:mi_mang_flutter/routers/fluro_navigator.dart';
import 'package:mi_mang_flutter/utils/image_utils.dart';
import 'package:mi_mang_flutter/utils/locale_util.dart';
import 'package:provider/provider.dart';

class SettingPage extends StatefulWidget {
  SettingPage({Key? key}) : super(key: key);

  @override
  _SettingPageState createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  @override
  void initState() {
    super.initState();
  }

  /// 总体布局
  Widget _buildBody() {
    return Consumer<UserProvider>(builder: (context, provider, _) {
      return ListView(
        padding: EdgeInsets.symmetric(vertical: Dimens.gapDp10),
        children: <Widget>[
          ItemNormalWidget.headItem(
            leading: Container(
              alignment: Alignment.center,
              decoration: BoxDecoration(
                border: Border.all(
                  color: ThemeUtil.isDark(context)
                      ? Color(0xFF5E5E5E)
                      : Color(0xFFEDECEB),
                  width: 5.0,
                ),
                shape: BoxShape.circle,
              ),
              child: ClipOval(
                child: loadNetworkImage(
                  provider.user?.avatar ?? '',
                  placeholder: "public/default_user",
                  width: 72.0,
                  height: 72.0,
                ),
              ),
            ),
            title: provider.user == null
                ? i18n(context).pleaseLogin
                : (provider.user?.nickName ?? i18n(context).editInfo),
            onTap: () {
              provider.user == null
                  ? NavigatorUtils.push(context, LoginRouter.login)
                  : NavigatorUtils.push(context, SettingRouter.personalInfo);
            },
          ),
          Gaps.vGap10,
          ...ItemNormalWidget.divideTiles(childs: <Widget>[
            ItemNormalWidget.item(
              title: i18n(context).accountSecurity,
              onTap: () {
                provider.user == null
                    ? NavigatorUtils.push(context, LoginRouter.login)
                    : NavigatorUtils.push(context, SettingRouter.account);
              },
            ),
            ItemNormalWidget.item(
              title: i18n(context).commonSetting,
              onTap: () {
                NavigatorUtils.push(context, SettingRouter.commonSetting);
              },
            ),
          ]).toList(),
          Gaps.vGap10,
          ...ItemNormalWidget.divideTiles(childs: <Widget>[
            ItemNormalWidget.item(
              title: i18n(context).giveGoodReputation,
              onTap: () {
                // LaunchReview.launch(
                //   androidAppId: "cn.yymimang.mimang",
                //   iOSAppId: "1518750424",
                // );
              },
            ),
            ItemNormalWidget.item(
              title: i18n(context).about,
              onTap: () {
                NavigatorUtils.push(context, SettingRouter.about);
              },
            ),
          ]).toList(),
          Gaps.vGap10,
          ItemNormalWidget.switchItem(
            title: i18n(context).nightMode,
            value: Theme.of(context).brightness == Brightness.dark,
            onChanged: (val) {
              ThemeProvider.switchDarkMode(context);
            },
          ),
          Gaps.vGap10,
          ...ItemNormalWidget.divideTiles(childs: <Widget>[
            ItemNormalWidget.item(
              title: i18n(context).feedback,
              onTap: () {
                NavigatorUtils.push(context, SettingRouter.feedback);
              },
            ),
          ]).toList(),
          Gaps.vGap10,
          provider.user != null
              ? Padding(
                  padding: EdgeInsets.symmetric(
                    horizontal: Dimens.gapDp15,
                    vertical: Dimens.gapDp15,
                  ),
                  child: ElevatedButton.icon(
                    icon: Icon(Icons.exit_to_app),
                    label: Text(i18n(context).loginOut),
                    onPressed: () {
                      AppManager.logOutDialog(context);
                    },
                  ),
                )
              : SizedBox.shrink(),
        ],
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(i18n(context).setting),
        centerTitle: true,
      ),
      body: SafeArea(
        child: _buildBody(),
      ),
    );
  }
}
